package com.nice.controller;

import com.alibaba.fastjson.JSON;
import com.nice.common.CacheUtils;
import com.nice.dao.ObjectDao;
import com.nice.entity.PriceSchema;
import com.nice.entity.ResponseResult;
import com.nice.entity.TradeSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

@Controller
public class TradeController {

    public final static int OPEN_TRADE_BUY = 1;
    public final static int OPEN_TRADE_SELL = 2;
    public final static int CLOSE_TRADE = 3;


    @Autowired
    ObjectDao objectDao;

    @PostMapping("/trade")
    @ResponseBody
    public String trade(@RequestParam String indexName, @RequestParam String lots, @RequestParam String type, @RequestParam String price, @RequestParam String seqNo) {
        ResponseResult responseResult = new ResponseResult();
        System.out.println(indexName);
        System.out.println(lots);
        System.out.println(type);
        System.out.println(price);
        if (StringUtils.isEmpty(indexName)||StringUtils.isEmpty(lots)||StringUtils.isEmpty(type)||StringUtils.isEmpty(price)) {
            responseResult.setResultCode(ResponseResult.FAIL);
            responseResult.setResultMsg("交易要素有问题");
            return JSON.toJSONString(responseResult);
        }
        TradeSchema tradeSchema = new TradeSchema();
        tradeSchema.setSeqNo(UUID.randomUUID().toString());
        tradeSchema.setIndexName(indexName);
        tradeSchema.setLots(Integer.valueOf(lots));
        tradeSchema.setType(Integer.valueOf(type));
        tradeSchema.setTradePrice(new BigDecimal(price));
        tradeSchema.setDate(new Date());
        if (TradeController.CLOSE_TRADE==(Integer.valueOf(type))) {
            if (seqNo == null || "".equals(seqNo)) {
                responseResult.setResultCode(ResponseResult.FAIL);
                responseResult.setResultMsg("seqNo不能为空，请选中要平仓的交易");
                return JSON.toJSONString(responseResult);
            }else{
                tradeSchema.setSeqNo(seqNo);
                objectDao.deleteTradeSchemaInfo(tradeSchema);
                return JSON.toJSONString(responseResult);
            }
        }else{
            objectDao.insertTradeSchemaInfo(tradeSchema);
            responseResult.setResultCode(ResponseResult.SUCCESS);
            return JSON.toJSONString(responseResult);
        }
    }

    @PostMapping("/querytrade")
    @ResponseBody
    public String querytrade() {
        Map<String, TradeSchema> map = objectDao.selectTradeSchemaInfo();
        List<TradeSchema> list = new ArrayList<>();
        if (map == null || map.isEmpty()) {
            return JSON.toJSONString(list);
        }
        //计算损益
        map.forEach(
                (k, v) -> {
                    TradeSchema tradeSchema = new TradeSchema();
                    tradeSchema = calProfit(v);
                    if (tradeSchema != null) {
                        list.add(tradeSchema);
                    }
                }
        );
        return JSON.toJSONString(list);
    }

    public TradeSchema calProfit(TradeSchema tradeSchema) {
        Map<String, PriceSchema> priceInfo = CacheUtils.priceInfo;
        PriceSchema priceSchema = priceInfo.get(tradeSchema.getIndexName());
        TradeSchema tradeSchema_new = new TradeSchema();
        //没有牌价就返回空
        if (priceSchema == null) {
            return null;
        }
        int type = tradeSchema.getType();
        if (type == this.OPEN_TRADE_BUY) {
            BigDecimal ask = new BigDecimal(priceSchema.getAsk());
            BigDecimal openPrice = tradeSchema.getTradePrice();
            BigDecimal profit = (ask.subtract(openPrice)).multiply(new BigDecimal(tradeSchema.getLots()));
            tradeSchema_new.setSeqNo(tradeSchema.getSeqNo());
            tradeSchema_new.setProfit(profit);
            tradeSchema_new.setIndexName(tradeSchema.getIndexName());
            tradeSchema_new.setDate(tradeSchema.getDate());
            tradeSchema_new.setTradePrice(tradeSchema.getTradePrice());
            tradeSchema_new.setType(tradeSchema.getType());
            tradeSchema_new.setLots(tradeSchema.getLots());
        }else if(type == this.OPEN_TRADE_SELL){
            BigDecimal bid = new BigDecimal(priceSchema.getBid());
            BigDecimal openPrice = tradeSchema.getTradePrice();
            BigDecimal profit = (openPrice.subtract(bid)).multiply(new BigDecimal(tradeSchema.getLots()));
            tradeSchema_new.setSeqNo(tradeSchema.getSeqNo());
            tradeSchema_new.setProfit(profit);
            tradeSchema_new.setIndexName(tradeSchema.getIndexName());
            tradeSchema_new.setDate(tradeSchema.getDate());
            tradeSchema_new.setTradePrice(tradeSchema.getTradePrice());
            tradeSchema_new.setType(tradeSchema.getType());
            tradeSchema_new.setLots(tradeSchema.getLots());
        }
        return tradeSchema_new;
    }

}
